//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//********************************************************* 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.TridentWordAddIn.Common;

namespace Microsoft.TridentWordAddIn.Presentation
{
    internal partial class UserNamePassword : UserControl
    {
        /// <summary>
        /// Occurs when [on validation].
        /// </summary>
        public event EventHandler<ConnectionValidateEventArgs> OnValidation;

        /// <summary>
        /// Initializes a new instance of the <see cref="UserNamePassword"/> class.
        /// </summary>
        public UserNamePassword()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Gets the name of the user.
        /// </summary>
        /// <value>The name of the user.</value>
        public string UserName
        {
            get
            {
                return this.txtUserName.Text;
            }
            set
            {
                this.txtUserName.Text = value;
            }
        }

        /// <summary>
        /// Gets the password.
        /// </summary>
        /// <value>The password.</value>
        public string Password
        {
            get
            {
                return this.txtPassword.Text;
            }
            set
            {
                this.txtPassword.Text = value;
            }
        }


        /// <summary>
        /// Clears the error providers.
        /// </summary>
        public void ClearErrorProviders()
        {
            if (this.OnValidation != null)
            {
                ValidationResult result = new ValidationResult(true);
                ConnectionValidateEventArgs conFormEventArgs = new ConnectionValidateEventArgs(result.GetMessage(), result.IsValid, "TextBox");
                OnValidation(txtUserName, conFormEventArgs);
                OnValidation(txtPassword, conFormEventArgs);
            }
        }

        /// <summary>
        /// Handles the Validating event of the txtUserName control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.ComponentModel.CancelEventArgs"/> instance containing the event data.</param>
        private void txtUserName_Validating(object sender, CancelEventArgs e)
        {
            if (this.OnValidation != null)
            {
                ValidationResult result = this.IsValidUserName();
                ConnectionValidateEventArgs conFormEventArgs = new ConnectionValidateEventArgs(result.GetMessage(), result.IsValid, "TextBox");
                OnValidation(txtUserName, conFormEventArgs);                
            }
        }

        /// <summary>
        /// Handles the Validating event of the txtPassword control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.ComponentModel.CancelEventArgs"/> instance containing the event data.</param>
        private void txtPassword_Validating(object sender, CancelEventArgs e)
        {
            if (this.OnValidation != null)
            {
                ValidationResult result = this.IsValidPassword();
                ConnectionValidateEventArgs conFormEventArgs = new ConnectionValidateEventArgs(result.GetMessage(), result.IsValid, "TextBox");
                OnValidation(txtPassword, conFormEventArgs);               
            }
        }

        /// <summary>
        /// Determines whether this instance is valid.
        /// </summary>
        /// <returns></returns>
        public ValidationResult IsValid()
        {
            ValidationResult result = this.IsValidUserName();
            if (!result.IsValid)
            {
                this.txtUserName_Validating(txtUserName, null);
            }

            ValidationResult passwordValidation = this.IsValidPassword();
            if (!passwordValidation.IsValid)
            {
                this.txtPassword_Validating(txtPassword, null);
            }

            result.Merge(passwordValidation);
            return result;
        }

        /// <summary>
        /// Determines whether [is user name valid].
        /// </summary>
        /// <returns></returns>
        private ValidationResult IsValidUserName()
        {
            ValidationResult result = new ValidationResult(!txtUserName.Text.Trim().IsNullOrEmpty());
            if (!result.IsValid)
            {
                result.Messages.Add("User name is required");
            }

            return result;
        }

        /// <summary>
        /// Determines whether [is password valid].
        /// </summary>
        /// <returns></returns>
        private ValidationResult IsValidPassword()
        {
            ValidationResult result = new ValidationResult(!txtPassword.Text.Trim().IsNullOrEmpty());
            if (!result.IsValid)
            {
                result.Messages.Add("Password is required");
            }

            return result;
        }
    }
}
